const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const PointTransaction = sequelize.define('PointTransaction', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  user_id: {
    type: DataTypes.UUID,
    allowNull: false,
    comment: '用户ID'
  },
  rule_id: {
    type: DataTypes.UUID,
    allowNull: false,
    comment: '积分规则ID'
  },
  points: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '积分数量'
  },
  type: {
    type: DataTypes.ENUM('earn', 'spend'),
    allowNull: false,
    comment: '积分类型'
  },
  description: {
    type: DataTypes.STRING,
    comment: '积分描述'
  },
  status: {
    type: DataTypes.ENUM('pending', 'completed', 'failed', 'cancelled'),
    defaultValue: 'pending',
    comment: '积分状态'
  },
  related_order_id: {
    type: DataTypes.UUID,
    comment: '关联订单ID'
  },
  related_payment_id: {
    type: DataTypes.UUID,
    comment: '关联支付ID'
  },
  balance_before: {
    type: DataTypes.INTEGER,
    comment: '积分变更前余额'
  },
  balance_after: {
    type: DataTypes.INTEGER,
    comment: '积分变更后余额'
  },
  metadata: {
    type: DataTypes.JSON,
    defaultValue: {},
    comment: '元数据'
  }
}, {
  underscored: true,
  indexes: [
    {
      fields: ['user_id'],
      comment: '用户ID索引'
    },
    {
      fields: ['rule_id'],
      comment: '积分规则ID索引'
    },
    {
      fields: ['related_order_id'],
      comment: '关联订单ID索引'
    },
    {
      fields: ['related_payment_id'],
      comment: '关联支付ID索引'
    }
  ],
  comment: '积分交易信息表'
});

module.exports = PointTransaction; 